package com.bjx.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.common.core.constant.FrameConst;
import com.bjx.common.core.constant.OrgConst;
import com.bjx.system.domain.entity.OrgEntity;
import com.bjx.system.domain.query.OrgQuery;
import com.bjx.system.domain.vo.OrgVO;
import com.bjx.system.mapper.OrgMapper;
import com.bjx.system.service.IOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @desc: 组织信息服务实现
 * @author: liushuai
 * @date: 2022/09/29
 */
@Service
public class OrgServiceImpl extends ServiceImpl<OrgMapper, OrgEntity> implements IOrgService {
    @Autowired
    private OrgMapper orgMapper;

    @Override
    public List<OrgVO> listOrg(OrgQuery orgQuery) {
        List<OrgVO> list = orgMapper.listOrg(orgQuery);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertOrg(OrgVO orgVO) {
        OrgEntity entity = Convert.convert(OrgEntity.class, orgVO);
        orgMapper.insert(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateOrg(OrgVO orgVO) {
        OrgEntity entity = Convert.convert(OrgEntity.class, orgVO);
         orgMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteOrgByOrgId(Long orgId) {
        orgMapper.deleteById(orgId);
    }

    @Override
    public OrgVO selectOrgByOrgId(Long orgId) {
        OrgEntity entity = orgMapper.selectById(orgId);
        OrgVO vo = Convert.convert(OrgVO.class,entity);
        return vo;
    }

    @Override
    public List<Tree<Long>> listTreeOrg(Long orgId, Boolean isCompany) {
        LambdaQueryWrapper<OrgEntity> wrapper = new LambdaQueryWrapper<>();
        if(isCompany){
            wrapper.eq(OrgEntity::getType, OrgConst.ORG_COMPANY);
        }
        List<OrgEntity> list = orgMapper.selectList(wrapper);
        List<Tree<Long>> treeNodes = TreeUtil.build(list, orgId, new TreeNodeConfig(),
                (treeNode, tree) -> {
                    tree.setId(treeNode.getOrgId());
                    if(isCompany){
                        tree.setParentId(treeNode.getComId());
                    }else{
                        tree.setParentId(treeNode.getPid());
                    }
                    tree.setWeight(treeNode.getOrderNum());
                    tree.setName(treeNode.getOrgName());
                    tree.putExtra("orgName", treeNode.getOrgName());
                    tree.putExtra("orgId",String.valueOf(treeNode.getOrgId()));
                    tree.putExtra("comId",String.valueOf(treeNode.getComId()));
                    tree.putExtra("type",treeNode.getType());
                    tree.putExtra("disabled",treeNode.getEnabled() != FrameConst.YES.intValue());
                });
        return treeNodes;
    }


}
